package com.etrans.baf.analyse.task.swap.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.etrans.baf.analyse.task.swap.service.AreaSwapService;
import com.etrans.baf.analyse.task.swap.service.EnterpriseSwapService;
import com.etrans.baf.analyse.util.CommonUtil;
import com.etrans.baf.analyse.util.Constants;
import com.etrans.baf.analyse.util.DateUtils;


@Component("areaSwapAnalyse")
@Controller
@RequestMapping("/task/swap/areaswap/*")
public class AreaSwapAnalyse {

	@Autowired
	private AreaSwapService areaSwapService;
	
	static final Logger logger = LoggerFactory.getLogger(AreaSwapAnalyse.class);

	@RequestMapping(value = "reTimer.html")
	public void reTimer(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {	
			logger.info("区域重分析开始----------------分析日期:"+request.getParameter("date"));
			String dateStr = (request.getParameter("date")!=null&& !"".equals(String.valueOf(request.getParameter("date"))))?String.valueOf(request.getParameter("date")):null;
			String analyse_date = DateUtils.getAnalyseDate(dateStr,0);
			areaSwapService.areaSwap(analyse_date);
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);

	}
	@RequestMapping(value = "reTimer1.html")
	public void reTimer1(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			logger.info("区域查岗重分析----------------分析日期:"+request.getParameter("date"));
			String dateStr = (request.getParameter("date")!=null&& !"".equals(String.valueOf(request.getParameter("date"))))?String.valueOf(request.getParameter("date")):null;
			String analyse_date = DateUtils.getAnalyseDate(dateStr,0);
			areaSwapService.areaCheck(analyse_date);
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}
	@RequestMapping(value = "reTimer2.html")
	public void reTimer2(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			logger.info("区域得分重开始分析----------------分析日期:"+request.getParameter("date"));
			String dateStr = (request.getParameter("date")!=null && !"".equals(String.valueOf(request.getParameter("date"))))?String.valueOf(request.getParameter("date")):null;
			String analyse_date = DateUtils.getAnalyseDate(dateStr,0);
			areaSwapService.updateScore(analyse_date);
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}
	@RequestMapping(value = "reAreaWeek.html")
	public void reAreaWeek(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			logger.info("区域周分析重开始分析-------某个星期一的日期:date:2017-09-25 ;分析日期date="+request.getParameter("date"));
			String start_date = (request.getParameter("date")!=null)?String.valueOf(request.getParameter("date")):DateUtils.getLastWeekOne(null);
 			String end_date = DateUtils.getLastWeekSeven(start_date);
			areaSwapService.areaWMSY(start_date, end_date, Constants.AREA_ANALYSE_W);
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}
	/*@RequestMapping(value = "reAreaWeek_score.html")
	public void reAreaWeek_score(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			logger.info("区域周分析得分重开始分析-------某个星期一的日期:date:2017-09-25 ;分析日期date="+request.getParameter("date"));
			String start_date = (request.getParameter("date")!=null)?String.valueOf(request.getParameter("date")):DateUtils.getLastWeekOne(null);
			areaSwapService.updateScoreWMSY(start_date, Constants.AREA_ANALYSE_W);
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}*/
	
	@RequestMapping(value = "reAreaMonth.html")
	public void reAreaMonth(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			logger.info("区域月分析重开始分析------last_mon:如2017-08,current_mon:如2017-09： last_mon="+request.getParameter("last_mon")+";current_mon="+request.getParameter("current_mon"));
			String last_mon =  (request.getParameter("last_mon")!=null && !"".equals(String.valueOf(request.getParameter("last_mon"))))?String.valueOf(request.getParameter("last_mon")):DateUtils.getAnalyseDate(null, 1).substring(0,7);
			String current_mon = (request.getParameter("current_mon")!=null&& !"".equals(String.valueOf(request.getParameter("current_mon"))))?String.valueOf(request.getParameter("current_mon")):DateUtils.format(new Date(),Constants.DATE_YYYY_MM);
			areaSwapService.areaWMSY(last_mon+"-25",current_mon+"-24", Constants.AREA_ANALYSE_M);
			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}
	/*@RequestMapping(value = "reAreaMonth_score.html")
	public void reAreaMonth_score(HttpServletRequest request,HttpServletResponse response){
		Map<String,String> map = new HashMap<String,String>();
		try {
			logger.info("区域月分析得分重分析-------分析月份：date_month格式:2017-09;参数:"+request.getParameter("date_month"));
			String current_mon = (request.getParameter("date_month")!=null && !"".equals(String.valueOf(request.getParameter("date_month"))))?String.valueOf(request.getParameter("date_month")):DateUtils.format(new Date(),Constants.DATE_YYYY_MM);
  			areaSwapService.updateScoreWMSY(current_mon+"-01", Constants.AREA_ANALYSE_M);
  			map.put("status", "ok");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("status", "error:"+e.getMessage());
		}
		CommonUtil.ajaxResultJson(response, map);
	}*/
	public void timer(){
		analyse();
	}
	public void timer1(){
		try {
			logger.info("区域查岗开始分析----------------");
			String analyse_date = DateUtils.getAnalyseDate(null,0);
			areaSwapService.areaCheck(analyse_date);
			logger.info("区域查岗分析结束");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("区域查岗错误------");
		}
		 
	}
	public void timer2(){
		try {
			logger.info("区域得分开始分析----------------");
			String analyse_date = DateUtils.getAnalyseDate(null,0);
			areaSwapService.updateScore(analyse_date);
			logger.info("区域得分分析结束");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void analyse(){
		logger.info("区域分析开始----------------");
		try {
			String analyse_date = DateUtils.getAnalyseDate(null,0);
			areaSwapService.areaSwap(analyse_date);
			logger.info("区域分析结束------------");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("区域分析错误-------------");
		}
	}
	//周分析
	public void areaWeek(){
		try {
			String start_date =DateUtils.getLastWeekOne(null);
			String end_date = DateUtils.getLastWeekSeven(start_date);
			areaSwapService.areaWMSY(start_date, end_date, Constants.AREA_ANALYSE_W);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 周分析得分
	public void areaWeek_score(){
		try {
			String start_date = DateUtils.getLastWeekOne(null);
			areaSwapService.updateScoreWMSY(start_date, Constants.AREA_ANALYSE_W);
 			//areaSwapService.updateScoreWMSY(start_date, Constants.AREA_ANALYSE_W);
 		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	//上月分析
	public void areaMonth(){
		try {
			/*String start_date = DateUtils.getLastMonthOne();
			String end_date = DateUtils.getLastMonthLastDay();*/
			String last_mon = DateUtils.getAnalyseDate(null, 1).substring(0,7);
			String current_mon = DateUtils.format(new Date(),Constants.DATE_YYYY_MM);
			areaSwapService.areaWMSY(last_mon+"-25",current_mon+"-24", Constants.AREA_ANALYSE_M);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 上月分析得分
	public void areaMonth_score(){
		try {
			String current_mon = DateUtils.format(new Date(),Constants.DATE_YYYY_MM);
  			areaSwapService.updateScoreWMSY(current_mon+"-01", Constants.AREA_ANALYSE_M);
 		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	//当月月分析
	public void areaCurMonth(){
		try {
			/*String current_mon = DateUtils.format(new Date(),Constants.DATE_YYYY_MM);
			String end_date = DateUtils.getCurMonthLastDay();
			areaSwapService.areaWMSY(current_mon+"-01",end_date, Constants.AREA_ANALYSE_CM);*/
			Date cdate = new Date();
			String current_mon_day = DateUtils.format(cdate,Constants.DATE_YYYY_MM_DD);
			String day = current_mon_day.substring(current_mon_day.length()-2,current_mon_day.length());
			String current_mon = DateUtils.format(cdate,Constants.DATE_YYYY_MM);
			if(Integer.parseInt(day)<=25){
				String last_mon = DateUtils.getAnalyseDate(null, 1).substring(0,7);
 				areaSwapService.areaWMSY(last_mon+"-25",current_mon+"-24", Constants.AREA_ANALYSE_CM);
			}else{
  				String next_month = DateUtils.nextMonthFirstDate().substring(0,7);
				areaSwapService.areaWMSY(current_mon+"-25",next_month+"-24", Constants.AREA_ANALYSE_CM);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/*//当月得分
	public void areaCurMonth_score(){
		try {
			String current_mon_day = DateUtils.format(new Date(),Constants.DATE_YYYY_MM_DD);
			String day = current_mon_day.substring(current_mon_day.length()-2,current_mon_day.length());
			if(Integer.parseInt(day)<=25){
				String current_mon = current_mon_day.substring(0,7);
	  			areaSwapService.updateScoreWMSY(current_mon+"-01",Constants.AREA_ANALYSE_CM);
			}else{
				areaSwapService.updateScoreWMSY(DateUtils.nextMonthFirstDate(),Constants.AREA_ANALYSE_CM);
			}
 		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
}
